package com.li.springbootproject.spring.beanfactorypostprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 3/12/2022 5:00 PM
 */
public class SpecialBeanForEngine implements BeanFactoryPostProcessor, BeanNameAware {

    public String name;

    @Override
    public void setBeanName(String name) {
        this.name = name;
    }

    /**
     * 注册 EngineFactory 的 BeanDefine
     *
     * @param beanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory;
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(EngineFactory.class);
        genericBeanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
        genericBeanDefinition.setAutowireCandidate(true);
        beanDefinitionRegistry.registerBeanDefinition("engine007", genericBeanDefinition);
    }
}
